package com.hopes.bizz.hazard.gis;

import com.hopes.bizz.hazard.alarm.entity.HazAlarmBrokenEntity;
import com.hopes.bizz.hazard.alarm.entity.HazAlarmOverproofEntity;
import com.hopes.bizz.hazard.alarm.service.HazAlarmBrokenService;
import com.hopes.bizz.hazard.alarm.service.HazAlarmOverproofService;
import com.hopes.bizz.hazard.enterprises.entity.HazEnterprisesMsgEntity;
import com.hopes.bizz.hazard.enterprises.entity.HazEnterprisesRtEntity;
import com.hopes.bizz.hazard.enterprises.service.HazEnterprisesMsgService;
import com.hopes.bizz.hazard.enterprises.service.HazEnterprisesRtService;
import com.hopes.bizz.hazard.om.service.HazRtDataService;
import com.hopes.bizz.hazard.supervise.entity.HazClassifySuperviseEntity;
import com.hopes.bizz.hazard.supervise.service.HazClassifySuperviseService;
import com.hopes.bizz.hazard.supervise.vo.HazClassifySuperviseGISVO;
import com.hopes.boss.admin.api.entity.SysCompanyEntity;
import com.hopes.boss.admin.api.feign.RemoteCompanyService;
import com.hopes.boss.common.core.constant.SecurityConstants;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author daijie
 * @version 1.0
 * @date 2024/2/20 13:58
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/gis")
@Tag(description = "gis", name = "重大危险源一张图")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazardGisController {

    private final HazEnterprisesRtService hazEnterprisesRtService;
    private final HazEnterprisesMsgService hazEnterprisesMsgService;
    private final HazClassifySuperviseService hazClassifySuperviseService;
    private final HazRtDataService hazRtDataService;
    private final HazAlarmOverproofService hazAlarmOverproofService;
    private final HazAlarmBrokenService hazAlarmBrokenService;
    private final RemoteCompanyService remoteCompanyService;

    /**
     * 查看企业实时风险
     *
     * @return R
     */
    @Operation(summary = "查看企业实时风险", description = "查看企业实时风险")
    @SysLog("查看企业实时风险")
    @GetMapping("enterprisesRt")
    public R enterprisesRt() {
        return R.ok(hazEnterprisesRtService.lambdaQuery().orderByAsc(HazEnterprisesRtEntity::getDateTime).list());
    }

    /**
     * 企业风险预警消息
     *
     * @return R
     */
    @Operation(summary = "企业风险预警消息", description = "企业风险预警消息")
    @SysLog("企业风险预警消息")
    @GetMapping("enterprisesMsg")
    public R enterprisesMsg() {
        return R.ok(hazEnterprisesMsgService.lambdaQuery().orderByAsc(HazEnterprisesMsgEntity::getAlarmTime).list());
    }

    /**
     * 企业分类监管
     *
     * @return R
     */
    @Operation(summary = "企业分类监管", description = "企业分类监管")
    @SysLog("企业分类监管")
    @GetMapping("classifySupervise")
    public R classifySupervise() {
        return R.ok(hazClassifySuperviseService.lambdaQuery().list());
    }

    /**
     * 传感器状态
     *
     * @return R
     */
    @Operation(summary = "传感器状态", description = "传感器状态")
    @SysLog("传感器状态")
    @GetMapping("monitorData")
    public R gisMonitorData() {
        return R.ok(hazRtDataService.gisMonitorData());
    }

    /**
     * 超标报警
     *
     * @return R
     */
    @Operation(summary = "超标报警", description = "超标报警")
    @SysLog("超标报警")
    @GetMapping("alarmOverproof")
    public R alarmOverproof() {
        return R.ok(hazAlarmOverproofService.lambdaQuery().orderByDesc(HazAlarmOverproofEntity::getAlarmTime).list());
    }

    /**
     * 断网报警
     *
     * @return R
     */
    @Operation(summary = "断网报警", description = "断网报警")
    @SysLog("断网报警")
    @GetMapping("alarmBroken")
    public R alarmBroken() {
        return R.ok(hazAlarmBrokenService.lambdaQuery().orderByDesc(HazAlarmBrokenEntity::getAlarmTime).list());
    }

    /**
     * 企业分类监管 图层
     *
     * @return R
     */
    @Operation(summary = "企业分类监管 图层", description = "企业分类监管 图层")
    @SysLog("企业分类监管 图层")
    @GetMapping("/layer/classifySupervise")
    public R classifySuperviseLayer() {
        List<SysCompanyEntity> companyList = remoteCompanyService.list(SecurityConstants.FROM_IN).getData();

        List<HazClassifySuperviseEntity> list = hazClassifySuperviseService.list();
        List<HazClassifySuperviseGISVO> result = new ArrayList<>();
        for (HazClassifySuperviseEntity superviseEntity : list) {
            HazClassifySuperviseGISVO vo = new HazClassifySuperviseGISVO();
            BeanUtils.copyProperties(superviseEntity, vo);

            Optional<SysCompanyEntity> first = companyList.stream().filter(it -> it.getId().equals(vo.getOrgId())).findFirst();
            if (first.isPresent()) {
                vo.setGisLocation(first.get().getGisLocation());
                vo.setGisScope(first.get().getGisScope());
            }
            result.add(vo);
        }

        return R.ok(result.stream()
                .collect(Collectors.groupingBy(HazClassifySuperviseGISVO::getSuperviseType)));
    }
}
